<template>
  <approvalView ref="approvalView" />
  <div class="app-table">
    <div class="app-body">
      <div class="app-search">
        <el-form :inline="true" class="demo-form-inline" v-model="form">
          <el-form-item label="起止时间" style="width: 320px">
            <el-date-picker v-model="form.duration" type="daterange" range-separator="To" start-placeholder="开始时间"
              end-placeholder="结束时间" value-format="YYYY-MM-DD" />
          </el-form-item>
          <el-form-item label="部门" style="width: 180px;">
            <el-input placeholder="请输入使用部门" v-model="form.userDepartment"></el-input>
          </el-form-item>
          <el-form-item label="姓名" style="width: 160px;">
            <el-input placeholder="请输入姓名" v-model="form.teacherName"></el-input>
          </el-form-item>
          <el-form-item label="状态" style="width: 200px;">
            <el-select placeholder="请选择审批状态" v-model="form.status" clearable>
              <el-option label="未审批" value="0"></el-option>
              <el-option label="已审批" value="1"></el-option>
              <el-option label="已上传" value="2"></el-option>
              <el-option label="已退回" value="4"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSearch" plain>搜索</el-button>
            <el-button type="success" @click="downTaskList" plain>下载</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="list" style="margin-bottom: 10px" :row-class-name="tableRowClassName">
        <el-table-column prop="id" label="序号" width="90" />
        <el-table-column prop="teacherName" label="姓名" width="100" />
        <el-table-column prop="userDepartment" label="使用部门" width="220" />
        <el-table-column prop="taskName" label="送稿内容" />
        <el-table-column prop="submitTime" label="上传日期" width="170" />
        <el-table-column prop="amount" label="数量" width="800" />
        <el-table-column prop="paperSize" label="纸张" width="100" />
        <el-table-column prop="status" label="状态" width="80" />
        <el-table-column label="操作" width="140px">
          <template #default="scope">
            <el-button size="small" type="success" @click="approval(scope.row)">审批</el-button>
            <el-button size="small" type="danger" v-if="scope.row.status !== '已审批'"
              @click="deleteApp(scope.row)">删除</el-button>
          </template>
        </el-table-column>
        <template v-slot:empty>
          <el-empty description="暂无数据"></el-empty>
        </template>
      </el-table>
      <el-config-provider :locale="locale">
        <el-pagination background="white" :total="total" :page-size="limit" :page-sizes="[8, 10, 20, 30, 40, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
          @current-change="changeCurrentPage">
        </el-pagination>
      </el-config-provider>
    </div>
    <bottomView></bottomView>
  </div>
</template>
<script>
import approvalView from "../../components/printingSystem/approvalPrintTask";
import bottomView from "../../components/bottom";

import { ElConfigProvider, ElMessageBox, ElMessage, ElDatePicker } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";

import { formatTime } from "../../utils/dateFormat";
import { httpService } from "../../utils/httpService";
import { Picture as IconPicture } from "@element-plus/icons-vue";
export default {
  name: "appListView",
  data() {
    return {
      list: [],
      page: 1,
      limit: 8,
      total: 0,
      value: "",
      pageCount: 0,
      form: {
        userDepartment: "",
        status: "",
        teacherName: "",
        duration: ""
      },
      startDate: "",
      endDate: "",
    };
  },
  watch: {
    'form.duration'(newValue) {
      if (newValue) {
        this.startDate = newValue[0]
        this.endDate = newValue[1]
      } else {
        this.startDate = ''
        this.endDate = ''
      }
    },
  },
  mounted() {
    // this.getTaskList();
    this.getDate();
    this.getPrintList();
  },

  components: {
    bottomView,
    approvalView,
    IconPicture,
    ElConfigProvider,
    ElMessageBox,
    ElMessage,
    ElDatePicker,
  },
  setup() {
    return {
      locale: zhCn,
    };
  },
  methods: {
    getDate() {
      const now = new Date();
      const year = now.getFullYear();
      const start = new Date(year, 0, 1);
      const end = new Date(year, 11, 31);
      this.startDate = formatTime(start);
      this.endDate = formatTime(end); 
    },
    getPrintList() {
      httpService(
        `/javaApi/printTaskForm/list/${this.page}/${this.limit}`,
        {
          status: this.form.status, teacherName: this.form.teacherName,
          userDepartment: this.form.userDepartment,
          // duration: this.form.duration,
          isDelete: 0, startDate: this.startDate, endDate: this.endDate,
        },
        "get"
      ).then((data) => {
        console.log(data);
        this.list = data.list
        this.total = data.total
      });
    },

    approval(obj) {
      // console.log(obj);
      this.$refs.approvalView.getPrintTaskFormById(obj.id);
      this.$refs.approvalView.title = "任务审批";
      this.$refs.approvalView.dialogFormVisible = true;
    },
    deleteApp(data) {
      console.log(data);
      ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = new FormData()
          params.append('id', data.id)
          params.append('isDelete', 1)
          httpService("/javaApi/printTaskForm/edit", params, "post").then((data) => {
            if (data.code == 200) {
              ElMessage.success("删除成功");
              this.getPrintList();
            } else {
              ElMessage.error("任务不存在或已审批");
            }
          });
        })
        .catch(() => { });
    },
    onSearch() {
      // this.getTaskList();
      this.getPrintList()
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.limit = size;
      this.getPrintList()
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page;
      this.getPrintList()
    },
    tableRowClassName({ row, rowIndex }) {
      return "";
    },

    downTaskList() {
      let config = { "responseType": "blob" }
      httpService(
        "/pythonApi/printingSystem/downApprovalData",
        {
          teacherName: this.form.teacherName, userDepartment: this.form.userDepartment,
          status: this.form.status, startDate: this.startDate, endDate: this.endDate
        }, "get", config).then((data) => {
          let fileName = '印刷明细'
          // let fileName = duration.toString();
          this.exportExcel(data, fileName)
        });
    },
    exportExcel(data, fileName) {
      let link = document.createElement('a'), fileUrl = window.URL.createObjectURL(new Blob([data]));
      link.style.display = 'none';
      link.href = fileUrl;
      link.setAttribute('download', fileName + '.xlsx');
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      window.URL.revokeObjectURL(fileUrl)
    },
    goPersonal() {
      this.$parent.power = 0;
    }
  },
};
</script>

<style scoped>
.app-table {
  background: #eff3f5;
  padding: 10px;
  width: calc(100vw - 220px);
}

.app-search {}


.app-body {
  background: white;
  padding: 20px 20px 10px 20px;
  margin-bottom: 10px;
  min-height: calc(100vh - 243px);
}
</style>
